package com.fcscs.cservice.client.main.reservation;

import java.util.List;

import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.FieldEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.Label;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.form.AdapterField;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.form.Validator;
import com.extjs.gxt.ui.client.widget.layout.ColumnData;
import com.extjs.gxt.ui.client.widget.layout.ColumnLayout;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.fcscs.core.gwt.common.AsyncCallbackListener;
import com.fcscs.core.gwt.common.CoreConstants;
import com.fcscs.core.gwt.common.CoreConstants.ValidationListener;
import com.fcscs.core.gwt.common.CoreEnums.SubmitType;
import com.fcscs.core.gwt.common.FormSubmitListener;
import com.fcscs.core.gwt.common.ui.BaseFormDialog;
import com.fcscs.core.gwt.common.ui.CenteredInfo;
import com.fcscs.core.gwt.common.ui.FcsFields.EasyClearCombobox;
import com.fcscs.core.gwt.common.ui.FcsFields.FormButton;
import com.fcscs.core.gwt.common.ui.FcsFormPanel;
import com.fcscs.cservice.client.common.CServiceConstants;
import com.fcscs.cservice.client.common.controller.GuestProfileServiceController;
import com.fcscs.cservice.client.common.controller.ReservationServiceController;
import com.fcscs.cservice.client.common.model.reservation.GenderEnum;
import com.fcscs.cservice.client.common.model.reservation.GenderGWTEnum;
import com.fcscs.cservice.client.common.model.reservation.GenderGWTEnumModelData;
import com.fcscs.cservice.client.common.model.reservation.LanguageModelData;
import com.fcscs.cservice.client.common.model.reservation.NationalityModelData;
import com.fcscs.cservice.client.common.model.reservation.ReservationViewModelData;
import com.fcscs.cservice.client.common.model.reservation.TitleModelData;
import com.fcscs.cservice.domain.model.CountryView;
import com.fcscs.cservice.domain.model.FolioView;
import com.fcscs.cservice.domain.model.GuestProfileView;
import com.fcscs.cservice.domain.model.RCView;
import com.fcscs.cservice.domain.model.ReservationConfView;
import com.fcscs.cservice.domain.model.ReservationConfView.Language;
import com.fcscs.cservice.domain.model.ReservationConfView.Nationality;
import com.fcscs.cservice.domain.model.ReservationConfView.Title;
import com.fcscs.cservice.domain.model.ReservationView;
import com.fcscs.share.gwt.common.ShareGWTConstants;

public class GuestProfileDialog extends BaseFormDialog<GuestProfileView> implements
		FormSubmitListener<GuestProfileView> {
	private MyFormPanel formPanel;
	private FormSubmitListener<GuestProfileView> listener;
	private static GuestProfileDialog INSTANCE;	
	private RegistrationCardDialog rcDialog;
	private FolioDialog foliodialog;
	private ReservationView rv;
	private ReservationConfView reservationConf;
	private List<CountryView> countryList;
	private boolean isFirst;
	private GuestProfileView currentGuest;
	private ReservationViewModelData rvModel;
	final String emailReg = "[A-Z0-9._%+-]+@[A-Z0-9-]+\\.[A-Z]{2,4}";
	
	public static GuestProfileDialog getInstance(FormSubmitListener<GuestProfileView> listener,
			ReservationViewModelData rvModel, ReservationConfView reservationConf, List<CountryView> countryList){
		if(null == INSTANCE){
			INSTANCE = new GuestProfileDialog();
		}
		INSTANCE.listener = listener;
		INSTANCE.rvModel = rvModel;
		INSTANCE.rv = rvModel.getEntity();
		INSTANCE.reservationConf = reservationConf;
		INSTANCE.countryList = countryList;
		INSTANCE.setHeading(CServiceConstants.CSERVICE_CONSTANTS.guestProfile());
		return INSTANCE;
	}
	
	private GuestProfileDialog(){
		formPanel = new MyFormPanel(this);
		formPanel.setId("GuestProfileDialog_formPanel");
		isFirst = true;
		add(formPanel);
		setSize(700, 290);
	}
	
	private class MyFormPanel extends FcsFormPanel<GuestProfileView>{
		private Label guestProfileNoLabel;
		private AdapterField guestProfileNoAF;
		
		private TextField<String> notesValueField;
		
		private Label vipCodeLabel;
		private AdapterField vipCodeAF;
		
		private EasyClearCombobox<LanguageModelData> languageCombo;
		private ListStore<LanguageModelData> languageListStore;
		
//		private Label guestNameLabel;
//		private AdapterField guestNameAF;
		
		private TextField<String> guestNameField;
		
		private EasyClearCombobox<NationalityModelData> nationalityCombo;
		private ListStore<NationalityModelData> nationalityListStore;
		
		private EasyClearCombobox<TitleModelData> titleCombo;
		private ListStore<TitleModelData> titleListStore;
		
		private TextField<String> emailValueField;
		
		private EasyClearCombobox<GenderGWTEnumModelData> genderCombo;
		private ListStore<GenderGWTEnumModelData> genderListStore;
		
		private TextField<String> mobileValueField;
		
//		private TextField<String> preferenceCodeValueField;
		private Label preferenceCodeLabel;
		private AdapterField preferenceCodeAF;
		
		private TextField<String> specialCodeValueField;
		
		private FormButton saveButton;
		private FormButton rcButton;
		private FormButton folioButton;
		private FormButton closeButton;
		
		private Validator specialCodeValidator = new Validator() {
			@Override
			public String validate(Field<?> field, final String code) {
				if(null != code && !(code.trim().isEmpty()) && code.trim().length()>200){
					return ShareGWTConstants.SHARE_GWT_MESSAGES.outOfLength(
							CServiceConstants.CSERVICE_CONSTANTS.specialCode(), 200);
				}
				return null;
			}
		};
		
		private Validator noteValidator = new Validator() {
			@Override
			public String validate(Field<?> field, final String note) {
				if(null != note && !(note.trim().isEmpty()) && note.trim().length()>500){
					return ShareGWTConstants.SHARE_GWT_MESSAGES.outOfLength(
							CServiceConstants.CSERVICE_CONSTANTS.notes(), 500);
				}
				return null;
			}
		};
		
		private Validator emailValidator = new Validator() {
			@Override
			public String validate(Field<?> field, final String value) {
				if(value == null){
					return CoreConstants.CORE_CONSTANTS.requiredField();
				}else if(value.trim().isEmpty()){
					return ShareGWTConstants.SHARE_GWT_CONSTANTS.cannotEmpty();
				}else if(value.trim().length()>100){
					return ShareGWTConstants.SHARE_GWT_MESSAGES.outOfLength(
							CoreConstants.CORE_CONSTANTS.email(), 100);
				}else if(!value.toUpperCase().matches(emailReg)){
					return ShareGWTConstants.SHARE_GWT_CONSTANTS.emailErro();
				}
				return null;
			}
		};
		
		private Validator mobileValidator = new Validator() {
			@Override
			public String validate(Field<?> field, final String value) {
				if(value == null){
					return CoreConstants.CORE_CONSTANTS.requiredField();
				}else if(value.trim().isEmpty()){
					return ShareGWTConstants.SHARE_GWT_CONSTANTS.cannotEmpty();
				}else if(value.trim().length()>50){
					return ShareGWTConstants.SHARE_GWT_MESSAGES.outOfLength(
							CoreConstants.CORE_CONSTANTS.mobile(), 50);
				}else if(!isValidMobile(value.trim())){
					return CServiceConstants.CSERVICE_CONSTANTS.enterNumber();
				}
				return null;
			}
		};

		protected MyFormPanel(FormSubmitListener<GuestProfileView> l) {
			super(l);
			setLabelAlign(LabelAlign.LEFT);
			setLayout(new ColumnLayout());
			setHeaderVisible(false);
			
			LayoutContainer left = new LayoutContainer();
			left.setId("GuestProfileDialog_left");
			
			FormLayout layout = new FormLayout();
			layout.setLabelAlign(LabelAlign.LEFT);
			layout.setLabelWidth(130);
			layout.setDefaultWidth(160);
			left.setLayout(layout);
			
			guestProfileNoLabel = new Label();
			guestProfileNoLabel.setId("GuestProfileDialog_guestProfileNoLabel");
			
			guestProfileNoAF = new AdapterField(guestProfileNoLabel);
			guestProfileNoAF.setId("GuestProfileDialog_guestProfileNoAF");
			guestProfileNoAF.setFieldLabel(CServiceConstants.CSERVICE_CONSTANTS.guestProfileNo());
			left.add(guestProfileNoAF);
			
			vipCodeLabel = new Label();
			vipCodeLabel.setId("GuestProfileDialog_vipCodeLabel");
			
			vipCodeAF = new AdapterField(vipCodeLabel);
			vipCodeAF.setId("GuestProfileDialog_vipCodeAF");
			vipCodeAF.setFieldLabel(CServiceConstants.CSERVICE_CONSTANTS.vipCodes());
			left.add(vipCodeAF);
			
//			guestNameLabel = new Label();
//			guestNameLabel.setId("GuestProfileDialog_guestNameLabel");
//			
//			guestNameAF = new AdapterField(guestNameLabel);
//			guestNameAF.setId("GuestProfileDialog_guestNameAF");
//			guestNameAF.setFieldLabel(ShareGWTConstants.SHARE_GWT_CONSTANTS.guestName());
//			left.add(guestNameAF);
			
			guestNameField = new TextField<String>();
			guestNameField.setId("GuestProfileDialog_guestNameField");
			guestNameField.setLabelSeparator(CoreConstants.REQURIED_FIELD_LABEL);
			guestNameField.setFieldLabel(ShareGWTConstants.SHARE_GWT_CONSTANTS.guestName());
			guestNameField.setAllowBlank(false);
			guestNameField.setValidator(new Validator(){
				@Override
				public String validate(Field<?> field, String value) {
					if(null == value){
						return CoreConstants.CORE_CONSTANTS.requiredField();
					}else if(value.trim().isEmpty()){
						return ShareGWTConstants.SHARE_GWT_CONSTANTS.cannotEmpty();
					}else if(!value.trim().contains(",")){
						return CServiceConstants.CSERVICE_CONSTANTS.validName();
					}else if(!isValidName(guestNameField.getValue().trim())){
						return ShareGWTConstants.SHARE_GWT_MESSAGES.outOfLength(CoreConstants.CORE_CONSTANTS.firstname() 
								+ ShareGWTConstants.SHARE_GWT_CONSTANTS.or()
								+ CoreConstants.CORE_CONSTANTS.lastname(), 255);
					}
					return null;
				}
			});
			left.add(guestNameField);
			
			titleListStore = new ListStore<TitleModelData>();
			titleCombo = new EasyClearCombobox<TitleModelData>();
			titleCombo.setId("GuestProfileDialog_titleCombo");
			titleCombo.setStore(titleListStore);
			titleCombo.setFieldLabel(ShareGWTConstants.SHARE_GWT_CONSTANTS.title());
			titleCombo.setDisplayField(CServiceConstants.TITLE);
			titleCombo.setTriggerAction(TriggerAction.ALL);
			titleCombo.setForceSelection(true);
			titleCombo.setEditable(false);
			left.add(titleCombo);
			
			genderListStore = new ListStore<GenderGWTEnumModelData>();
			for(GenderGWTEnum ge : GenderGWTEnum.values()){
				genderListStore.add(new GenderGWTEnumModelData(ge));
			}
			genderCombo = new EasyClearCombobox<GenderGWTEnumModelData>();
			genderCombo.setId("GuestProfileDialog_genderCombo");
			genderCombo.setStore(genderListStore);
			genderCombo.setFieldLabel(CServiceConstants.CSERVICE_CONSTANTS.gender());
			genderCombo.setDisplayField(CoreConstants.FULLTNAME);
			genderCombo.setTriggerAction(TriggerAction.ALL);
			genderCombo.setForceSelection(true);
			genderCombo.setEditable(false);
			left.add(genderCombo);
			
//			preferenceCodeValueField = new TextField<String>();
//			preferenceCodeValueField.setId("GuestProfileDialog_preferenceCodeValueField");
//			preferenceCodeValueField.setFieldLabel(CServiceConstants.CSERVICE_CONSTANTS.perferenceCode());
//			preferenceCodeValueField.setAllowBlank(false);
//			left.add(preferenceCodeValueField);
			
			preferenceCodeLabel = new Label();
			preferenceCodeLabel.setId("GuestProfileDialog_preferenceCodeLabel");
			
			preferenceCodeAF = new AdapterField(preferenceCodeLabel);
			preferenceCodeAF.setId("GuestProfileDialog_preferenceCodeAF");
			preferenceCodeAF.setFieldLabel(CServiceConstants.CSERVICE_CONSTANTS.preferenceCode());
			left.add(preferenceCodeAF);
			
			specialCodeValueField = new TextField<String>();
			specialCodeValueField.setId("GuestProfileDialog_specialCodeValueField");
			specialCodeValueField.setFieldLabel(CServiceConstants.CSERVICE_CONSTANTS.specialCode());
			specialCodeValueField.setValidator(specialCodeValidator);
			specialCodeValueField.addListener(Events.OnBlur,new Listener<FieldEvent>() {
				@Override
				public void handleEvent(FieldEvent be) {
					String value = specialCodeValueField.getValue();
					if(null != value && !(value.trim().isEmpty()) && value.trim().length()>200){
						specialCodeValueField.forceInvalid(ShareGWTConstants.SHARE_GWT_MESSAGES.outOfLength(
								CServiceConstants.CSERVICE_CONSTANTS.specialCode(), 200));
						return;
					}else{
						specialCodeValueField.clearInvalid();
						return;
					}
				}
			});
			left.add(specialCodeValueField);
			
			LayoutContainer right = new LayoutContainer();
			right.setId("GuestProfileDialog_right");
			
			layout = new FormLayout();
			layout.setLabelAlign(LabelAlign.LEFT);
			layout.setLabelWidth(130);
			layout.setDefaultWidth(160);
			right.setLayout(layout);
			
			notesValueField = new TextField<String>();
			notesValueField.setId("GuestProfileDialog_notesValueField");
			notesValueField.setFieldLabel(CServiceConstants.CSERVICE_CONSTANTS.notes());
			notesValueField.setValidator(noteValidator);
			notesValueField.addListener(Events.OnBlur,new Listener<FieldEvent>() {
				@Override
				public void handleEvent(FieldEvent be) {
					String value = notesValueField.getValue();
					if(null != value && !(value.trim().isEmpty()) && value.trim().length()>500){
						notesValueField.forceInvalid(ShareGWTConstants.SHARE_GWT_MESSAGES.outOfLength(
								CServiceConstants.CSERVICE_CONSTANTS.notes(), 500));
						return;
					}else{
						notesValueField.clearInvalid();
						return;
					}
				}
			});
			right.add(notesValueField);
			
			languageListStore = new ListStore<LanguageModelData>();
			languageCombo = new EasyClearCombobox<LanguageModelData>();
			languageCombo.setId("GuestProfileDialog_languageCombo");
			languageCombo.setStore(languageListStore);
			languageCombo.setFieldLabel(CoreConstants.CORE_CONSTANTS.language());
			languageCombo.setDisplayField(ShareGWTConstants.LANGUAGE);
			languageCombo.setTriggerAction(TriggerAction.ALL);
			languageCombo.setForceSelection(true);
			languageCombo.setEditable(false);
			right.add(languageCombo);
			
			nationalityListStore = new ListStore<NationalityModelData>();
			nationalityCombo = new EasyClearCombobox<NationalityModelData>();
			nationalityCombo.setId("GuestProfileDialog_nationalityCombo");
			nationalityCombo.setStore(nationalityListStore);
			nationalityCombo.setFieldLabel(ShareGWTConstants.SHARE_GWT_CONSTANTS.nationality());
			nationalityCombo.setDisplayField(CServiceConstants.NATIONALITY);
			nationalityCombo.setTriggerAction(TriggerAction.ALL);
			nationalityCombo.setForceSelection(true);
			nationalityCombo.setEditable(false);
			right.add(nationalityCombo);
			
			emailValueField = new TextField<String>();
			emailValueField.setId("GuestProfileDialog_emailValueField");
			emailValueField.setLabelSeparator(CoreConstants.REQURIED_FIELD_LABEL);
			emailValueField.setFieldLabel(ShareGWTConstants.SHARE_GWT_CONSTANTS.email());
			emailValueField.setAllowBlank(false);
			emailValueField.setValidator(emailValidator);
			emailValueField.addListener(Events.OnBlur, new  Listener<BaseEvent>(){
				@Override
				public void handleEvent(BaseEvent be) {
					String value = emailValueField.getValue();
					if(value == null){
						emailValueField.forceInvalid(CoreConstants.CORE_CONSTANTS.requiredField());
						return;
					}else if(value.trim().isEmpty()){
						emailValueField.forceInvalid(ShareGWTConstants.SHARE_GWT_CONSTANTS.cannotEmpty());
						return;
					}else if(value.trim().length()>100){
						emailValueField.forceInvalid(ShareGWTConstants.SHARE_GWT_MESSAGES.outOfLength(CoreConstants.CORE_CONSTANTS.email(), 100));
						return;
					}else if(!value.toUpperCase().matches(emailReg)){
						emailValueField.forceInvalid(ShareGWTConstants.SHARE_GWT_CONSTANTS.emailErro());
						return;
					}else{
						emailValueField.clearInvalid();
						return;
					}
				}
			});
			right.add(emailValueField);
			
			
			mobileValueField = new TextField<String>();
			mobileValueField.setId("GuestProfileDialog_mobileValueField");
			mobileValueField.setLabelSeparator(CoreConstants.REQURIED_FIELD_LABEL);
			mobileValueField.setFieldLabel(ShareGWTConstants.SHARE_GWT_CONSTANTS.mobile());
			mobileValueField.setValidator(mobileValidator);
			mobileValueField.addListener(Events.OnBlur,new Listener<FieldEvent>() {
				@Override
				public void handleEvent(FieldEvent be) {
					String value = mobileValueField.getValue();
					if(value == null){
						mobileValueField.forceInvalid(CoreConstants.CORE_CONSTANTS.requiredField());
						return;
					}else if(value.trim().isEmpty()){
						mobileValueField.forceInvalid(ShareGWTConstants.SHARE_GWT_CONSTANTS.cannotEmpty());
						return;
					}else if(value.trim().length()>50){
						mobileValueField.forceInvalid(ShareGWTConstants.SHARE_GWT_MESSAGES.outOfLength(CoreConstants.CORE_CONSTANTS.mobile(), 50));
						return;
					}else if(!isValidMobile(value.trim())){
						mobileValueField.forceInvalid(CServiceConstants.CSERVICE_CONSTANTS.enterNumber());
						return;
					}else{
						mobileValueField.clearInvalid();
						return;
					}
				}
			});
			
			right.add(mobileValueField);
			
			saveButton = new FormButton(CoreConstants.CORE_CONSTANTS.save());
			saveButton.setId("GuestProfileDialog_saveButton");
			saveButton.addSelectionListener(new SelectionListener<ButtonEvent>(){
				@Override
				public void componentSelected(ButtonEvent ce) {
					isValid(new ValidationListener() {

						@Override
						public void onValidate(boolean good) {
							if(good){
								populateEntity(currentGuest);
								GuestProfileServiceController.getINSTANCE().updateGuestProfileView(
									currentGuest, new AsyncCallbackListener<GuestProfileView>(){

										@Override
										public void onLogin(String username) {
											GuestProfileServiceController.getINSTANCE().updateGuestProfileView(
													currentGuest,this);
										}

										@Override
										public void onFailure(Throwable caught) {
											MessageBox.alert(
													CoreConstants.CORE_MESSAGES.operation(
														CoreConstants.CORE_CONSTANTS.system(), 
														ShareGWTConstants.SHARE_GWT_CONSTANTS.error()),
													CoreConstants.CORE_CONSTANTS.systemError(), 
													null);
										}

										@Override
										public void onSuccess(GuestProfileView result) {
											if(null != result){
												ReservationViewModelData rvmd = new ReservationViewModelData(rv);
												ReservationPanel.grid.replaceModelData(rvModel, rvmd);
												rvModel = rvmd;
												CenteredInfo.displayCompleted(CoreConstants.CORE_CONSTANTS.edited());
											}
										}
										
									});
							}
						}
					});
				}
			});
			
			rcButton = new FormButton(CServiceConstants.CSERVICE_CONSTANTS.registrationCard());
			rcButton.setId("GuestProfileDialog_rcButton");
			rcButton.addSelectionListener(new SelectionListener<ButtonEvent>(){
				@Override
				public void componentSelected(ButtonEvent ce) {
					ReservationServiceController.getINSTANCE().getRCView(
						rv.getRcs().get(0).getRcId(),
						new AsyncCallbackListener<RCView>(){
							@Override
							public void onLogin(String username) {
								ReservationServiceController.getINSTANCE().getRCView(
									rv.getRcs().get(0).getRcId(), this);
							}
							@Override
							public void onFailure(Throwable caught) {
								MessageBox.alert(
									CoreConstants.CORE_MESSAGES.operation(
										CoreConstants.CORE_CONSTANTS.system(), 
										ShareGWTConstants.SHARE_GWT_CONSTANTS.error()),
									CoreConstants.CORE_CONSTANTS.systemError(), 
									null);
							}
							@Override
							public void onSuccess(final RCView result) {
								if (null != result) {
									rcDialog = RegistrationCardDialog.getInstance(
										new FormSubmitListener<RCView>() {
											@Override
											public void formSubmitCallBack(FcsFormPanel<RCView> form,
												SubmitType submitType) {
												guestNameField.setValue(rcDialog.guestNameField.getValue());
												titleCombo.setValue(rcDialog.titleCombo.getValue());
												genderCombo.setValue(rcDialog.genderCombo.getValue());
												nationalityCombo.setValue(rcDialog.nationalityCombo.getValue());
												emailValueField.setValue(rcDialog.emailField.getValue());
												mobileValueField.setValue(rcDialog.mobilField.getValue());
												
												rcDialog.getFormPanel().populateEntity(result);
												ReservationServiceController.getINSTANCE().updateRC(
														result, new AsyncCallbackListener<RCView>(){
															@Override
															public void onLogin(String username) {
																ReservationServiceController.getINSTANCE().updateRC(
																		result, this);								
															}
															@Override
															public void onFailure(Throwable caught) {
																MessageBox.alert(
																		CoreConstants.CORE_MESSAGES.operation(
																			CoreConstants.CORE_CONSTANTS.system(), 
																			ShareGWTConstants.SHARE_GWT_CONSTANTS.error()),
																		CoreConstants.CORE_CONSTANTS.systemError(), 
																		null);
															}
															@Override
															public void onSuccess(RCView result) {
																if(result != null){
																	ReservationViewModelData rvmd = new ReservationViewModelData(rv);
																	ReservationPanel.grid.replaceModelData(rvModel, rvmd);
																	rvModel = rvmd;
																	
																	rcDialog.hide();
																	CenteredInfo.displayCompleted(CoreConstants.CORE_CONSTANTS.edited());
																}
															}
														});
											}
										}, rvModel, reservationConf, countryList, true);
									rcDialog.getFormPanel().updateForm(result);
									rcDialog.show();
								}
							}
					});
				}
			});
			
			
			folioButton = new FormButton(CServiceConstants.CSERVICE_CONSTANTS.folio());
			folioButton.setId("GuestProfileDialog_folioButton");
			folioButton.addSelectionListener(new SelectionListener<ButtonEvent>(){
				@Override
				public void componentSelected(ButtonEvent ce) {
					if(rv.getFolioId() != null){
					ReservationServiceController.getINSTANCE().getFolioView(
						rv.getFolioId(), 
						new AsyncCallbackListener<FolioView>(){
							@Override
							public void onLogin(String username) {
								ReservationServiceController.getINSTANCE().getFolioView(
										rv.getFolioId(),this);
							}

							@Override
							public void onFailure(Throwable caught) {
								MessageBox.alert(
										CoreConstants.CORE_MESSAGES.operation(
											CoreConstants.CORE_CONSTANTS.system(), 
											ShareGWTConstants.SHARE_GWT_CONSTANTS.error()),
										CoreConstants.CORE_CONSTANTS.systemError(), 
										null);
							}

							@Override
							public void onSuccess(FolioView result) {
								foliodialog = FolioDialog.getINSTANCE(reservationConf);
								foliodialog.getFormPanel().updateForm(result);
								foliodialog.show();
							}
							
						});
					}else{
						MessageBox.alert(
								"",
								CServiceConstants.CSERVICE_MESSAGES.thereIsNo(CServiceConstants.CSERVICE_CONSTANTS.folio()), 
								null);
					}
				}
			});
			
			closeButton = new FormButton(CoreConstants.CORE_CONSTANTS.cancel());
			closeButton.setId("GuestProfileDialog_closeButton");
			closeButton.addSelectionListener(new SelectionListener<ButtonEvent>(){
				@Override
				public void componentSelected(ButtonEvent ce) {
					GuestProfileDialog.this.hide();
				}
			});
			
			add(left, new ColumnData(.5));
			add(right, new ColumnData(.5));
			
			getButtonBar().add(saveButton);
			getButtonBar().add(rcButton);
			getButtonBar().add(folioButton);
			getButtonBar().add(closeButton);
		}

		@Override
		public void updateForm(GuestProfileView entity) {
			currentGuest = entity;
			guestProfileNoLabel.setText(entity.getProfileNumber());
			vipCodeLabel.setText(entity.getVipCodes());
			guestNameField.setValue(entity.getFirstName() + "," + entity.getLastName());
			genderCombo.setValue(entity.getGenderMale() 
					? genderListStore.findModel(CoreConstants.ID, GenderEnum.MALE.toString())
					: genderListStore.findModel(CoreConstants.ID, GenderEnum.FEMALE.toString()));
			preferenceCodeLabel.setText(entity.getPrefCodes());
			specialCodeValueField.setValue(entity.getSpecialCodes());
			notesValueField.setValue(entity.getNotes());
			emailValueField.setValue(entity.getEmail());
			mobileValueField.setValue(entity.getMobile());
			
			for(Title title : reservationConf.getTitleList()){
				titleListStore.add(new TitleModelData(title));
			}
			titleCombo.setValue(titleListStore.findModel(CoreConstants.ID, entity.getTitleId()));
			
			for(Language lang : reservationConf.getLanguageList()){
				languageListStore.add(new LanguageModelData(lang));
			}
			languageCombo.setValue(languageListStore.findModel(CoreConstants.ID, entity.getLanguageId()));
			
			for(Nationality nat: reservationConf.getNationalityList()){
				nationalityListStore.add(new NationalityModelData(nat));
			}
			nationalityCombo.setValue(nationalityListStore.findModel(CoreConstants.ID, entity.getNationalityId()));			
		}

		@Override
		public void populateEntity(GuestProfileView entity) {
			String strs = guestNameField.getValue();
			entity.setFirstName(strs.split(",")[0].trim());
			entity.setLastName(strs.split(",")[1].trim());
			rv.setGuestFirstName(strs.split(",")[0].trim());
			rv.setGuestLastName(strs.split(",")[1].trim());
			
			entity.setTitleId(titleCombo.getValue().getEntity().getTitleId());
			entity.setGenderMale(genderCombo.getValue().getEnum().equals(GenderGWTEnum.MALE));
			if (null != specialCodeValueField.getValue()) {
				entity.setSpecialCodes(specialCodeValueField.getValue().trim());
			}else{
				entity.setSpecialCodes(null);
			}
			if (null != notesValueField.getValue()) {
				entity.setNotes(notesValueField.getValue());
			}else{
				entity.setNotes(null);
			}
			entity.setLanguageId(languageCombo.getValue().getEntity().getLanguageId());
			entity.setNationalityId(nationalityCombo.getValue().getEntity().getNationalityId());
			entity.setEmail(emailValueField.getValue().trim());
			if (null != mobileValueField.getValue()) {
				entity.setMobile(mobileValueField.getValue().trim());
			}else{
				entity.setMobile(null);
			}
		}

		@Override
		public void setFocus() {
			guestNameField.focus();
		}
		
	}

	@Override
	public void formSubmitCallBack(FcsFormPanel<GuestProfileView> form,
			SubmitType submitType) {
		if (listener != null) {
			listener.formSubmitCallBack(form, submitType);
		}
	}

	@Override
	protected FcsFormPanel<GuestProfileView> getFormPanel() {
		return formPanel;
	}

	@Override
	public void hide() {
		super.hide();
		if(!isFirst){
			formPanel.titleListStore.removeAll();
			formPanel.languageListStore.removeAll();
			formPanel.nationalityListStore.removeAll();
			
			formPanel.guestNameField.clearInvalid();
			formPanel.titleCombo.clearInvalid();
			formPanel.genderCombo.clearInvalid();
			formPanel.specialCodeValueField.clearInvalid();
			formPanel.notesValueField.clearInvalid();
			formPanel.languageCombo.clearInvalid();
			formPanel.nationalityCombo.clearInvalid();
			formPanel.emailValueField.clearInvalid();
			formPanel.mobileValueField.clearInvalid();
		}
		isFirst = false;
	}
	
	private boolean isValidName(String name){
		boolean isValid = true;
		String[] ns = name.split(",");
		String firstName = ns[0].trim();
		String lastName = ns[1].trim();
		if(firstName.length()>255 || lastName.length()>255){
			isValid = false;
		}
		return isValid;
	}
	
	@SuppressWarnings("deprecation")
	private boolean isValidMobile(String mobile){
		boolean isValid = true;
		for(int i = 0; i< mobile.substring(1).length(); i++){
			if(!Character.isDigit(mobile.substring(1).charAt(i))
			   && !Character.isSpace(mobile.substring(1).charAt(i))){
				isValid = false;
				break;
			}
		}
		return isValid;
	}
}
